package controller;

import java.io.File;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import pojo.Houselist;
import pojo.User;
import service.HouselistService;

@Controller

public class HoustlistController {
	@Autowired
	private HouselistService houselistService;

	@RequestMapping("/houselist")
	public String houselist(Model model, @RequestParam(required = false, defaultValue = "1") Integer page,
			@RequestParam(required = false, defaultValue = "3") Integer pageSize) {

		PageHelper.startPage(page, pageSize);
		List<Houselist> houselist = houselistService.selectEnableHouses();
		PageInfo<Houselist> p = new PageInfo<Houselist>(houselist);

		model.addAttribute("p", p);
		model.addAttribute("houselist", houselist);
		model.addAttribute("mainPage", "houselist.jsp");
		return "zuke/main";
	}

	@RequestMapping("/ahouselist")
	public String ahouselist(Model model, @RequestParam(required = false, defaultValue = "1") Integer page,
			@RequestParam(required = false, defaultValue = "3") Integer pageSize, HttpSession httpSession) {
		PageHelper.startPage(page, pageSize);
		model.addAttribute("mainPage", "ahouselist.jsp");
		User loginUser = (User) httpSession.getAttribute("loginUser");
		String userType = loginUser.getType();
		if ("admin".equals(userType)) {
			List<Houselist> houselist = houselistService.selectAll();
			PageInfo<Houselist> p = new PageInfo<Houselist>(houselist);
			model.addAttribute("houselist", houselist);
			model.addAttribute("p", p);
			return "admin/main";
		} else {
			Integer user_id = loginUser.getId();
			List<Houselist> houselist = houselistService.selectMyEnaleHouses(user_id);
			PageInfo<Houselist> p = new PageInfo<Houselist>(houselist);
			model.addAttribute("houselist", houselist);
			model.addAttribute("p", p);
			return "fangdong/main";
		}
	}

	@RequestMapping("/addhouse")
	public String addhouse(Model model, Houselist houselist, MultipartFile picture, HttpSession session)
			throws Exception {
		String originalFilename = picture.getOriginalFilename();
		if (picture.getSize() > 0 && originalFilename != null && originalFilename.length() > 0) {
			String newFileName = UUID.randomUUID() + ".jpg";
			File file = new File(session.getServletContext().getRealPath(""));
			String parentPath = file.getParent();
			String uploadPath = parentPath + "/images";
			File uploadFile = new File(uploadPath);
			if (!uploadFile.exists()) {
				uploadFile.mkdirs();
			}
			File realPath = new File(uploadPath, newFileName);
			picture.transferTo(realPath);
			houselist.setPictureUrl(newFileName);
		}
		
		User user = (User) session.getAttribute("loginUser");
		String houseid = houselist.getHouseid();
		Houselist houselist1 = houselistService.findhouseid(houseid);
		if (houselist1 != null) {
			model.addAttribute("error", "该房屋编号已存在");
			model.addAttribute("houselist", houselist);
			model.addAttribute("mainPage", "addhouse.jsp");
			return "fangdong/main";
		} else {
			model.addAttribute("error", "添加成功，请等待管理员审核");
			houselist.setBelong(user.getId());
			houselist.setEnable(0);
			houselistService.inserthouse(houselist);
			model.addAttribute("houselist", houselist);
			model.addAttribute("mainPage", "addhouse.jsp");
			return "fangdong/main";
		}
	}

	@RequestMapping("/toaddhouse")
	public String toaddhoust(Model model) {
		model.addAttribute("mainPage", "addhouse.jsp");
		return "fangdong/main";
	}

	@RequestMapping("/deletehouse")
	public String deletehouse(Integer id) {
		houselistService.deletehouse(id);
		return "redirect:ahouselist.action";
	}

	@RequestMapping("/enablehouse")
	public String enablehouse(Integer id) {
		Houselist houselist = houselistService.findid(id);
		houselist.setEnable(1);
		houselistService.updatehouse(houselist);
		return "redirect:ahouselist.action";
	}

	@RequestMapping("/unablehouse")
	public String unablehouse(Integer id) {
		Houselist houselist = houselistService.findid(id);
		houselist.setEnable(-1);
		houselistService.updatehouse(houselist);
		return "redirect:ahouselist.action";
	}

	@RequestMapping("/toahouselist")
	public String toahouselist() {
		return "ahouselist.action";
	}

	@RequestMapping("/findid")
	public String findid(Integer id, Model model) {
		Houselist list = houselistService.findid(id);
		model.addAttribute("houselist", list);
		model.addAttribute("mainPage", "changehouse.jsp");
		return "fangdong/main";
	}

	@RequestMapping("/findhouseidupdate")
	public String findhouseidupdate(Houselist houselist, Model model, MultipartFile picture, HttpSession session)
			throws Exception {
		String originalFilename = picture.getOriginalFilename();
		if (picture.getSize() > 0 && originalFilename != null && originalFilename.length() > 0) {
			String newFileName = UUID.randomUUID() + ".jpg";
			File file = new File(session.getServletContext().getRealPath(""));
			String parentPath = file.getParent();
			String uploadPath = parentPath + "/images";
			File uploadFile = new File(uploadPath);
			if (!uploadFile.exists()) {
				uploadFile.mkdirs();
			}
			File realPath = new File(uploadPath, newFileName);
			picture.transferTo(realPath);
			houselist.setPictureUrl(newFileName);
		}

		Houselist list = houselistService.findhouseidupdate(houselist);
		if (list != null) {
			model.addAttribute("houselist", houselist);
			model.addAttribute("mainPage", "changehouse.jsp");
			model.addAttribute("error", "该房屋id已存在");
			return "fangdong/main";
		} else {
			houselistService.updatehouse(houselist);
			model.addAttribute("houselist", houselist);
			model.addAttribute("mainPage", "changehouse.jsp");
			model.addAttribute("error", "更新成功");
			return "fangdong/main";
		}
	}

}
